<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="description" content="Pytorch，Python 深度学习库中文文档。">
  <meta name="author" content="Rogerspy">
  <link rel="canonical" href="https://pytorch-zh.gitee.io/">
  <link rel="shortcut icon" href="/pytorch-zh/img/favicon.ico">
  <title>Pytorch 中文文档（1.4.0）</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700" />

  <link rel="stylesheet" href="css/theme.css" />
  <link rel="stylesheet" href="css/theme_extra.css" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css" />
  
  <script>
    // Current page data
    var mkdocs_page_name = "\u4e3b\u9875";
    var mkdocs_page_input_path = "index.md";
    var mkdocs_page_url = "/";
  </script>
  
  <script src="js/jquery-2.1.1.min.js" defer></script>
  <script src="js/modernizr-2.8.3.min.js" defer></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script> 
  
</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
    <div class="wy-side-scroll">
      <div class="wy-side-nav-search">
	    <img src='/pytorch-zh/img/logo.svg'/>
        <a href="." class="icon icon-home"> Pytorch 中文文档（1.4.0）</a>
        <div role="search">
  <form id ="rtd-search-form" class="wy-form" action="./search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" title="Type search term here" />
  </form>
</div>
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
                <ul class="current">
                    <li class="toctree-l1 current"><a class="reference internal current" href=".">主页</a>
    <ul class="current">
    <li class="toctree-l2"><a class="reference internal" href="#pytorch-zh">关于pytorch-zh</a>
    </li>
    <li class="toctree-l2"><a class="reference internal" href="#_2">版本与更新</a>
    </li>
    <li class="toctree-l2"><a class="reference internal" href="#_3">安装指引</a>
    </li>
    </ul>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="get-started/">60分钟快速入门</a>
                    </li>
                </ul>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="faq/">PyTorch FAQ</a>
                    </li>
                </ul>
                <p class="caption"><span class="caption-text">两种基本结构</span></p>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="torchnn/parameters/">参数 Parameters</a>
                    </li>
                    <li class="toctree-l1"><a class="reference internal" href="#">容器 Containers</a>
    <ul>
                <li class="toctree-l2"><a class="reference internal" href="torchnn/module/">Module</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="torchnn/sequential/">Sequential</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="torchnn/modulelist/">Modulelist</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="torchnn/moduledict/">Moduledict</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="torchnn/parameterlist/">Parameterlist</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="torchnn/parameterdict/">Parameterdict</a>
                </li>
    </ul>
                    </li>
                </ul>
                <p class="caption"><span class="caption-text">网络层</span></p>
                <ul>
                    <li class="toctree-l1"><a class="reference internal" href="#">卷积层</a>
    <ul>
                <li class="toctree-l2"><a class="reference internal" href="torchnn/conv1d/">Conv1d</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="torchnn/conv2d/">Conv2d</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="torchnn/conv3d/">Conv3d</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="torchnn/convtranspose1d/">Convtranspose1d</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="torchnn/convtranspose2d/">Convtranspose2d</a>
                </li>
                <li class="toctree-l2"><a class="reference internal" href="torchnn/convtranspose3d/">Convtranspose3d</a>
                </li>
    </ul>
                    </li>
                </ul>
      </div>
    </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href=".">Pytorch 中文文档（1.4.0）</a>
      </nav>

      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href=".">Docs</a> &raquo;</li>
    
      
    
    <li>主页</li>
    <li class="wy-breadcrumbs-aside">
      
    </li>
  </ul>
  
  <hr/>
</div>
          <div role="main">
            <div class="section">
              
                <p><img alt="" src="/pytorch-zh/img/logo-fill.svg" /></p>
<h1 id="_1">前言<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h1>
<h2 id="pytorch-zh">关于pytorch-zh<a class="headerlink" href="#pytorch-zh" title="Permanent link">&para;</a></h2>
<p>PyTorch是一个基于Torch的Python开源机器学习库，用于自然语言处理等应用程序。它主要由Facebook 的人工智能小组开发，不仅能够实现强大的GPU加速，同时还支持动态神经网络，这一点是现在很多主流框架如TensorFlow都不支持的。 </p>
<p>PyTorch提供了两个高级功能： </p>
<ol>
<li>具有强大的GPU加速的张量计算（如Numpy） </li>
<li>包含自动求导系统的深度神经网络 </li>
</ol>
<p>除了Facebook之外，Twitter、GMU和Salesforce等机构都采用了PyTorch。本文档是Pytorch的中文文档，包括了所有Pytorch API的介绍，除此以外，本文档还加入了官方教程的内容，方便读者更好的了解相关API的应用。</p>
<p>实际上Pytorch目前已经提供了中文版文档：<a href="https://pytorch.apachecn.org/">PyTorch中文文档</a>。写本文档出于两个目的：</p>
<ul>
<li>学习Pytorch。我觉得在学习任何一个框架的时候，没有什么比直接看API+例子更快的了，本文档一来可以当做操作手册使用，二来也算是我的学习笔记吧。</li>
<li>我之前很喜欢Keras框架，首先是Keras本身是一个非常优秀的框架，正如宣传的那样：“Keras 是为人类而不是为机器设计的 API”，其简单易用性为深度学习新手提供了一个非常好的平台；其次，Keras的官方文档是我看过的最喜欢的文档，没有之一，因此本文的设计逻辑是尽量遵照Keras文档来设计。</li>
</ul>
<p>由于作者水平有限，无法对所有模块都非常精通，因此文档中不可避免的会出现各种错误、疏漏和不足之处。如果您在使用过程中有任何意见、建议和疑问，欢迎发送邮件到rogerspy@163.com与我取得联系。您对文档的任何贡献，包括文档的翻译、查缺补漏、概念解释、发现和修改问题、贡献示例程序等，均会被记录在**致谢**，十分感谢您对本文档的贡献！</p>
<h2 id="_2">版本与更新<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h2>
<p>如果你发现本文档提供的信息有误，有两种可能：</p>
<ul>
<li>你的Pytorch版本过低：请保持你的Pytorch与官方最新的release版本相符；</li>
<li>我的中文文档没有及时更新：如果是这种情况，请发邮件给我，我会尽快更新。</li>
</ul>
<p>目前文档的版本是1.4.0，对应官方的1.4 release版本。</p>
<h2 id="_3">安装指引<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h2>
<p>Pytorch最新版本是1.4.0，因此这里我们以这个版本的安装为例进行介绍，如果想要老版本的Pytorch可以按照<a href="https://pytorch.org/get-started/previous-versions">旧版Pytorch安装</a>提供的方法进行安装。</p>
<ul>
<li><strong>操作系统</strong></li>
</ul>
<p>目前Pytorch支持的操作系统有Windows、Linux、MacOS。</p>
<p>Windows 支持 Windows7 及以上版本，建议 Windows10 及以上版本以及 Windows Server 2008 r2 及以上版本。Linux 支持使用 glibc &gt;= v2.17 库的系统，其中包括：</p>
<table>
<thead>
<tr>
<th align="center">系统</th>
<th align="center">最低版本</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">Arch Linux</td>
<td align="center">2012-07-15</td>
</tr>
<tr>
<td align="center">CentOS</td>
<td align="center">7.3-16 11</td>
</tr>
<tr>
<td align="center">Debian</td>
<td align="center">8.0</td>
</tr>
<tr>
<td align="center">Fedora</td>
<td align="center">24</td>
</tr>
<tr>
<td align="center">Mint</td>
<td align="center">14</td>
</tr>
<tr>
<td align="center">OpenSUSE</td>
<td align="center">42.1</td>
</tr>
<tr>
<td align="center">PCLinuxOS</td>
<td align="center">2014.7</td>
</tr>
<tr>
<td align="center">Slackware</td>
<td align="center">14.2</td>
</tr>
<tr>
<td align="center">Ubuntu</td>
<td align="center">13.04</td>
</tr>
</tbody>
</table>
<p>​                       </p>
<ul>
<li><strong>Python</strong></li>
</ul>
<p>Pytorch 兼容 python 2.x 和 python 3.x，但是推荐使用 python 3.x，尤其是 python 3.6 及以上的版本。因为 python 2.x 官方已经停止维护，另外 Windows 操作系统中 Pytroch 已经不支持 python 2.x 了，所以尽量使用 python 3.6 以上的编程环境。</p>
<ul>
<li><strong>CPU/GPU</strong></li>
</ul>
<p>Pytorch提供CPU版和GPU版本。建议使用带GPU的电脑安装，毕竟深度学习训练模型的过程非常耗时，没有GPU用来训练模型将会是一件非常痛苦的事情，但这并非必要的。</p>
<p>如果想使用GPU，那么必须提前安装GPU驱动、CUDA以及CudNN。</p>
<ul>
<li><strong>Anaconda/pip</strong></li>
</ul>
<p>Python 的包管理工具推荐使用Anaconda （<a href="https://www.anaconda.com/download/#windows">下载入口</a>），不仅集成了绝大多数数据分析必备的包，而且管理起来也非常方便。</p>
<p>由于一些不可描述的原因，国内安装一些python包的时候速度太慢，我们可以在 conda 中添加国内镜像达到加速下载安装的目的。</p>
<p>下面以添加清华镜像源为例：</p>
<div class="codehilite"><pre><span></span><code>conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
</code></pre></div>

<p>Pytorch提供了三种安装方式：conda、pip、源码：</p>
<ul>
<li>conda 安装</li>
</ul>
<div class="codehilite"><pre><span></span><code><span class="c1"># Linux + CUDA 9.2</span>
conda install pytorch torchvision <span class="nv">cudatoolkit</span><span class="o">=</span><span class="m">9</span>.2 -c pytorch

<span class="c1"># Linux/Windows + CUDA 10.1</span>
conda install pytorch torchvision <span class="nv">cudatoolkit</span><span class="o">=</span><span class="m">10</span>.1 -c pytorch

<span class="c1"># Windows + CUDA 9.2</span>
conda install pytorch torchvision <span class="nv">cudatoolkit</span><span class="o">=</span><span class="m">9</span>.2 -c pytorch -c defaults -c numba/label/dev

<span class="c1"># Linux/Windows none CUDA</span>
conda install pytorch torchvision cpuonly -c pytorch

<span class="c1"># MacOS none CUDA</span>
conda install pytorch torchvision -c pytorch

<span class="c1"># MacOS 二进制文件不支持CUDA，如果像用CUDA，必须从源码进行安装</span>
</code></pre></div>

<ul>
<li>pip 安装</li>
</ul>
<div class="codehilite"><pre><span></span><code><span class="c1"># Linux/Windows + CUDA 9.2</span>
pip install <span class="nv">torch</span><span class="o">==</span><span class="m">1</span>.4.0+cu92 <span class="nv">torchvision</span><span class="o">==</span><span class="m">0</span>.5.0+cu92 -f https://download.pytorch.org/whl/torch_stable.html

<span class="c1"># Linux + CUDA 10.1</span>
pip install torch torchvision

<span class="c1"># Windows + CUDA 10.1</span>
pip install <span class="nv">torch</span><span class="o">===</span><span class="m">1</span>.4.0 <span class="nv">torchvision</span><span class="o">===</span><span class="m">0</span>.5.0 -f https://download.pytorch.org/whl/torch_stable.html

<span class="c1"># Linux/Windows  none CUDA</span>
pip install <span class="nv">torch</span><span class="o">==</span><span class="m">1</span>.4.0+cpu <span class="nv">torchvision</span><span class="o">==</span><span class="m">0</span>.5.0+cpu -f https://download.pytorch.org/whl/torch_stable.html

<span class="c1"># MacOS none CUDA</span>
pip install torch torchvision

<span class="c1"># MacOS 二进制文件不支持CUDA，如果像用CUDA，必须从源码进行安装</span>
</code></pre></div>

<ul>
<li>源码安装</li>
</ul>
<p>按照指示进行安装：<a href="https://github.com/pytorch/pytorch#from-source">https://github.com/pytorch/pytorch#from-source</a></p>
<p>除了在本地安装使用之外，Pytorch还提供了一些云服务平台合作伙伴：</p>
<ul>
<li><a href="https://data.aliyun.com/bigdata/pai-pytorch?spm=5176.12825654.a9ylfrljh.d112.7b652c4ayuOO4M&amp;scm=20140722.1068.1.1098&amp;aly_as=-PvJ5e4c">Alibaba Cloud</a></li>
<li><a href="https://aws.amazon.com/pytorch/">Amazon</a></li>
<li><a href="https://cloud.google.com/deep-learning-vm/docs/pytorch_start_instance">Google Cloud Deep Learning VM Image</a></li>
<li><a href="https://cloud.google.com/ai-platform/deep-learning-containers/">Google Deep Learning Containers</a></li>
<li><a href="https://azure.microsoft.com/en-us/services/machine-learning-service/">Microsoft Azure Machine Learning</a></li>
<li><a href="https://azure.microsoft.com/en-us/develop/pytorch/">Microsoft Azure Learn More</a></li>
</ul>
<p>另外，pytorch 1.4还提供了可在手机上运行的版本：</p>
<ul>
<li>
<p>Get started on <a href="https://pytorch.org/mobile/android">Android</a></p>
</li>
<li>
<p>Get started on <a href="https://pytorch.org/mobile/ios">iOS</a></p>
</li>
</ul>
<p>为了确保已经正确安装了pytorch，我们需要进行简单的验证：</p>
<ol>
<li>
<p>命令行输入<code>python</code></p>
</li>
<li>
<p>在python consle中输入：</p>
</li>
</ol>
<div class="codehilite"><pre><span></span><code><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="kn">import</span> <span class="nn">torch</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</code></pre></div>

<p>如果能够正确输出：</p>
<div class="codehilite"><pre><span></span><code><span class="n">tensor</span><span class="p">([[</span><span class="mf">0.3380</span><span class="p">,</span> <span class="mf">0.3845</span><span class="p">,</span> <span class="mf">0.3217</span><span class="p">],</span>
        <span class="p">[</span><span class="mf">0.8337</span><span class="p">,</span> <span class="mf">0.9050</span><span class="p">,</span> <span class="mf">0.2650</span><span class="p">],</span>
        <span class="p">[</span><span class="mf">0.2979</span><span class="p">,</span> <span class="mf">0.7141</span><span class="p">,</span> <span class="mf">0.9069</span><span class="p">],</span>
        <span class="p">[</span><span class="mf">0.1449</span><span class="p">,</span> <span class="mf">0.1132</span><span class="p">,</span> <span class="mf">0.1375</span><span class="p">],</span>
        <span class="p">[</span><span class="mf">0.4675</span><span class="p">,</span> <span class="mf">0.3947</span><span class="p">,</span> <span class="mf">0.1426</span><span class="p">]])</span>
</code></pre></div>

<p>则说明我们已经正确安装了pytorch，接下来就可以愉快的玩耍了。</p>
              
            </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="get-started/" class="btn btn-neutral float-right" title="60分钟快速入门">Next <span class="icon icon-circle-arrow-right"></span></a>
      
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <!-- Copyright etc -->
    
      <p>©2020 Rogerspy. All rights reserved.</p>
    
  </div>

  Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
      
        </div>
      </div>

    </section>

  </div>

  <div class="rst-versions" role="note" aria-label="versions">
    <span class="rst-current-version" data-toggle="rst-current-version">
      
      
      
        <span style="margin-left: 15px"><a href="get-started/" style="color: #fcfcfc">Next &raquo;</a></span>
      
    </span>
</div>
    <script>var base_url = '.';</script>
    <script src="js/theme.js" defer></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML" defer></script>
      <script src="search/main.js" defer></script>
    <script defer>
        window.onload = function () {
            SphinxRtdTheme.Navigation.enable(true);
        };
    </script>

</body>
</html>

<!--
MkDocs version : 1.1
Build Date UTC : 2020-11-04 14:27:40
-->
